package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.mapper.RoleMapper;
import com.itany.pojo.Role;
import com.itany.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2019-09-25 上午10:05
 * Description：<描述>
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Throwable.class)
public class RoleServiceImpl implements RoleService {


    @Autowired
    private RoleMapper roleMapper;


    @Transactional(readOnly = true)
    public PageInfo<Role> findAllBook(Integer page, Integer rows, Role role) {

        PageHelper.startPage(page, rows);
        List<Role> books = roleMapper.selectAllRole(role);
        PageInfo<Role> info = new PageInfo<Role>(books);

        return info;
    }

    /**
     * 角色修改
     * @param role
     * @param split
     */
    public void modifyRole(Role role, String[] split) {

        roleMapper.updateRole(role);
       roleMapper.deletePermissionByRoleId(role.getId());
        for (int i=0;i<split.length;i++){

            roleMapper.insertPermissionByRoleId(role.getId(),Integer.parseInt(split[i]));

        }


    }

    /**
     * 通过role Id 进行权限id的查询
     * @param id
     * @return
     */
    public List<Integer> findPermissionByRoleId(Integer id) {
        List<Integer> integers = roleMapper.selectPermissionByRoleId(id);
        return integers;


    }

    public void removeRoleById(Integer id) {
        List<Integer> integers = roleMapper.selectPermissionByRoleId(id);
        if(null!=integers){
            roleMapper.deletePermissionByRoleId(id);
            roleMapper.deleteRoleById(id);
        }else{
            roleMapper.deleteRoleById(id);
        }



    }
    @Transactional(readOnly = true)
    public List<Role> findAllRole() {
        Role role = new Role();
        List<Role> roles = roleMapper.selectAllRole(role);
        return roles;
    }
}